/**
 * 
 */
package br.gov.sjc.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import br.gov.sjc.classes.DadosPessoais;
import br.gov.sjc.classes.Documentos;

/**
 * @author rodrigo.ramos
 *
 */
public class DocumentosDAO extends SQLiteOpenHelper {
	private static final String TABELA = "tab_Documentos";
	private static final String DATABASE_NAME = BaseDAO.DATABASE_NAME;
	private static final int DATABASE_VERSION = BaseDAO.DATABASE_VERSION;
	private static final String[] COLS = {"_id", "SiasN", "idMembro", "RG","RGExp","RGUF","RGDataExp","CPF","CPFDataExp","CTPS","CTPSSerie","CTPSDataExp","CTPSUF","NIS"};
	
	public DocumentosDAO(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE IF NOT EXISTS " + TABELA + 
		"(_id INTEGER PRIMARY KEY autoincrement," +
		" SiasN INTEGER," + 
		" idMembro INTEGER," +
		" RG TEXT," +
		" RGExp TEXT," + 
		" RGUF TEXT, " +
		" RGDataExp TEXT, " +
		" CPF TEXT, " +
		" CPFDataExp TEXT, " +
		" CTPS TEXT, " +
		" CTPSSerie TEXT, " +
		" CTPSDataExp TEXT, " +
		" CTPSUF TEXT, " +
		" NIS TEXT " +
		");";
		db.execSQL(sql);
		/**/
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + DocumentosDAO.TABELA);
		this.onCreate(db);/**/
	}
	
	public void inserir(Documentos docs){ 
		ContentValues valores = new ContentValues();
		valores.put("SiasN", docs.getSiasN());
		valores.put("idMembro", docs.getIdMembro());
		valores.put("RG", docs.getRG());
		valores.put("RGExp", docs.getRGExp());
		valores.put("RGUF", docs.getRGUF());
		valores.put("RGDataExp", docs.getRGDataExp());
		valores.put("CPF", docs.getCPF());
		valores.put("CPFDataExp", docs.getCPFDataExp());
		valores.put("CTPS", docs.getCTPS());
		valores.put("CTPSSerie", docs.getCTPSSerie());
		valores.put("CTPSDataExp", docs.getCTPSDataExp());
		valores.put("CTPSUF", docs.getCTPSUF());
		valores.put("NIS", docs.getNIS());
		
		getWritableDatabase().insert(TABELA, null, valores);
	}
	
	public List<Documentos> getLista(){
		Cursor c = getWritableDatabase().query(TABELA, COLS, null, null, null, null, null);
		List<Documentos> lista = new ArrayList<Documentos>();
		while(c.moveToNext()){
			Documentos docs = new Documentos();
			docs.set_id(c.getInt(0));
			docs.setSiasN(c.getInt(1));
			docs.setIdMembro(c.getInt(2));
			docs.setRG(c.getString(3));
			docs.setRGExp(c.getString(4));
			docs.setRGUF(c.getString(5));
			docs.setRGDataExp(c.getString(6));
			docs.setCPF(c.getString(7));
			docs.setCPFDataExp(c.getString(8));
			docs.setCTPS(c.getString(9));
			docs.setCTPSSerie(c.getString(10));
			docs.setCTPSDataExp(c.getString(11));
			docs.setNIS(c.getString(12));
			
			lista.add(docs);
		}
		c.close();
		
		return lista;
		
	}

	public Documentos getById(int posicao) {
		Cursor c = getWritableDatabase().query(TABELA, COLS, "_id=?", new String[]{"" + posicao}, null, null, null);
		c.moveToFirst();
		
		Documentos docs = new Documentos();
		docs.set_id(c.getInt(0));
		docs.setSiasN(c.getInt(1));
		docs.setIdMembro(c.getInt(2));
		docs.setRG(c.getString(3));
		docs.setRGExp(c.getString(4));
		docs.setRGUF(c.getString(5));
		docs.setRGDataExp(c.getString(6));
		docs.setCPF(c.getString(7));
		docs.setCPFDataExp(c.getString(8));
		docs.setCTPS(c.getString(9));
		docs.setCTPSSerie(c.getString(10));
		docs.setCTPSDataExp(c.getString(11));
		docs.setNIS(c.getString(12));
		
		c.close();
		return docs;
	}

	public void alterar(Documentos docs) {
		ContentValues valores = new ContentValues();
		valores.put("SiasN", docs.getSiasN());
		valores.put("idMembro", docs.getIdMembro());
		valores.put("RG", docs.getRG());
		valores.put("RGExp", docs.getRGExp());
		valores.put("RGUF", docs.getRGUF());
		valores.put("RGDataExp", docs.getRGDataExp());
		valores.put("CPF", docs.getCPF());
		valores.put("CPFDataExp", docs.getCPFDataExp());
		valores.put("CTPS", docs.getCTPS());
		valores.put("CTPSSerie", docs.getCTPSSerie());
		valores.put("CTPSDataExp", docs.getCTPSDataExp());
		valores.put("CTPSUF", docs.getCTPSUF());
		valores.put("NIS", docs.getNIS());
		
		getWritableDatabase().update(TABELA, valores, "_id=?", new String[]{"" + docs.get_id()});
		
	}

	public boolean recreateTable(SQLiteDatabase database) {
		boolean retorno = false;
		
		try {
			database.execSQL("DROP TABLE IF EXISTS " + TABELA);
	    	onCreate(database);
	    	retorno = true;
		}catch(Exception e){
			Log.e(TABELA,"ERRO MENSAGEM:"+e.getMessage());
        }
		
		
		return retorno;
	}

}
